package cn.wolfcode.wolf2w.strategy.service.impl;

import cn.wolfcode.wolf2w.strategy.api.domain.Strategy;
import cn.wolfcode.wolf2w.strategy.api.domain.StrategyCondition;
import cn.wolfcode.wolf2w.strategy.mapper.StrategyConditionMapper;
import cn.wolfcode.wolf2w.strategy.query.StrategyConditionQuery;
import cn.wolfcode.wolf2w.strategy.service.IStrategyConditionService;
import cn.wolfcode.wolf2w.strategy.service.IStrategyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 攻略条件Service业务层处理
 * 
 * @author dafei
 * @date 2023-11-25
 */
@Service
@Transactional
public class StrategyConditionServiceImpl extends ServiceImpl<StrategyConditionMapper,StrategyCondition> implements IStrategyConditionService {


    @Autowired
    private IStrategyService strategyService;
    @Override
    public IPage<StrategyCondition> queryPage(StrategyConditionQuery qo) {
        IPage<StrategyCondition> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyCondition> queryCondtionByType(int type) {
//        select * from ta_strategy_condition
//        where type = 3 and
//        statis_time in (select max(statis_time) from ta_strategy_condition where type = 3)
//        order by count desc

        return lambdaQuery().eq(StrategyCondition::getType, type)
                .inSql(StrategyCondition::getStatisTime, "(select max(statis_time) from ta_strategy_condition where type = "+type+")")
                .orderByDesc(StrategyCondition::getCount).list();
    }

    @Override
    public void strategyConditionHandle(int type) {

        //查询大表
        //select theme_id refid, theme_name name , count(id) count from ta_strategy group by theme_id theme_name order by count desc



        //缓存到小表

        QueryWrapper<Strategy> wrapper = new QueryWrapper<>();
        if(type == StrategyCondition.TYPE_THEME){
            wrapper.select("theme_id refid, theme_name name , count(id) count");
            wrapper.groupBy("theme_id, theme_name");
        }else{
            wrapper.select("dest_id refid, dest_name name , count(id) count");
            wrapper.eq("isabroad", StrategyCondition.TYPE_ABROAD==type?1:0);
            wrapper.groupBy("dest_id, dest_name");
        }
        wrapper.orderByDesc("count");
        Date now = new Date();
        List<Map<String, Object>> maps = strategyService.listMaps(wrapper);
        List<StrategyCondition> scs = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            StrategyCondition sc = new StrategyCondition();
            sc.setCount(Integer.parseInt(map.get("count").toString()));
            sc.setName(map.get("name").toString());
            sc.setType(type);
            sc.setStatisTime(now);
            sc.setRefid(Long.parseLong(map.get("refid").toString()));
            scs.add(sc);
        }
        super.saveBatch(scs);
    }
}
